<script setup lang="tsx">
import { Form, FormSchema } from '@/components/Form'
import { PropType, reactive, ref } from 'vue'
import { useValidator } from '@/hooks/web/useValidator'
import { SysAccountDetailVo } from '@/api/system/account/SysAccount/types'
import { isBlank } from '@/utils/StringUtils'
import { useBaseWriteView } from '@/base/views/BaseWriteView'
import { sysDictChooseListApi } from '@/api/system/dict/SysDict'
import { KeyLabelValueVo } from '@/base/api/KeyLabelValueVo'
import { rsaEncrypt } from '@/utils/RsaEncrypt'
import {selectSysRoleNameListApi} from "@/api/system/role/SysRole";
// [[ OnflowGenerator Range (- selectFunctionImport -) Start ]]

// [[ OnflowGenerator Range (- selectFunctionImport -) End ]]

const { required } = useValidator()

const props = defineProps({
  currentRow: {
    type: Object as PropType<Nullable<SysAccountDetailVo>>,
    default: () => null
  },
  editType: {
    type: String,
    default: 'add'
  },
  editId: {
    type: Number,
    default: -1
  }
})
const isAdd = ref(props.editType === 'add')

const { formRegister, submit, doWatch } = useBaseWriteView(props, (data) => {
  if (!isBlank(data.password)) {
    data.password = rsaEncrypt(data.password)
  }
})
doWatch()

const formRef = ref<typeof Form>()
// [[ OnflowGenerator Range (- dictFunctions -) Start ]]
const genderOptions = ref<Array<KeyLabelValueVo>>([])
const getGender = async () => {
  const res = await sysDictChooseListApi('gender')
  genderOptions.value = res.data
}
getGender()

// [[ OnflowGenerator Range (- dictFunctions -) End ]]

// [[ OnflowGenerator Range (- selectFunctions -) Start ]]

// [[ OnflowGenerator Range (- selectFunctions -) End ]]

const sysRoleNameList = ref<Array<KeyLabelValueVo>>([])
const getSysRoleNameList = async () => {
  const res = await selectSysRoleNameListApi({})
  sysRoleNameList.value = res.data
}
getSysRoleNameList()

const schema = reactive<FormSchema[]>([
  // [[ OnflowGenerator Range (- inputSchema -) Start ]]
  {
    field: 'name',
    label: '账户名',
    component: 'Input',
    componentProps: {
      placeholder: '请输入账户名',
      clearable: true
    }
  },
  {
    field: 'uid',
    label: '账户UID',
    component: 'Input',
    componentProps: {
      placeholder: '请输入账户UID',
      clearable: true,
      disabled: true
    }
  },
  {
    field: 'nickName',
    label: '昵称',
    component: 'Input',
    componentProps: {
      placeholder: '请输入昵称',
      clearable: true
    }
  },
  {
    field: 'password',
    label: '密码',
    component: 'Input',
    componentProps: {
      placeholder: '请输入密码',
      clearable: true,
      type: 'password',
      showPassword: true
    }
  },
  {
    field: 'roleId',
    label: '角色',
    component: 'Select',
    componentProps: {
      placeholder: '请选择角色id',
      clearable: true,
      filterable: true,
      options: sysRoleNameList
    }
  },
  {
    field: 'gender',
    label: '性别',
    component: 'Select',
    componentProps: {
      placeholder: '请选择性别',
      clearable: true,
      filterable: true,
      options: genderOptions
    }
  },
  {
    field: 'locked',
    label: '是否被锁定',
    value: false,
    component: 'Switch',
    componentProps: {
      placeholder: '请选择是否被锁定'
    }
  },
  {
    field: 'mobile',
    label: '手机号',
    component: 'Input',
    componentProps: {
      placeholder: '请输入手机号',
      clearable: true
    }
  },
  {
    field: 'email',
    label: '邮箱',
    component: 'Input',
    componentProps: {
      placeholder: '请输入邮箱',
      clearable: true
    }
  },
  {
    field: 'avatar',
    label: '头像',
    component: 'Input',
    componentProps: {
      placeholder: '请输入头像',
      clearable: true
    }
  },
  {
    field: 'country',
    label: '国家',
    component: 'Input',
    componentProps: {
      placeholder: '请输入国家',
      clearable: true
    }
  },
  {
    field: 'province',
    label: '省份',
    component: 'Input',
    componentProps: {
      placeholder: '请输入省份',
      clearable: true
    }
  },
  {
    field: 'city',
    label: '城市',
    component: 'Input',
    componentProps: {
      placeholder: '请输入城市',
      clearable: true
    }
  },
  {
    field: 'address',
    label: '地址',
    component: 'Input',
    componentProps: {
      placeholder: '请输入地址',
      clearable: true
    }
  },
  {
    field: 'birthday',
    label: '生日',
    component: 'DatePicker',
    componentProps: {
      placeholder: '请选择生日',
      clearable: true,
      type: 'datetime',
      valueFormat: 'YYYY-MM-DD HH:mm:ss'
    }
  },
  {
    field: 'registerDate',
    label: '注册日期',
    component: 'DatePicker',
    remove: isAdd.value,
    componentProps: {
      placeholder: '请选择注册日期',
      clearable: true,
      type: 'datetime',
      valueFormat: 'YYYY-MM-DD HH:mm:ss',
      disabled: true
    }
  },
  {
    field: 'lastLoginDate',
    label: '上次登录日期',
    component: 'DatePicker',
    remove: isAdd.value,
    componentProps: {
      placeholder: '请选择上次登录日期',
      clearable: true,
      type: 'datetime',
      valueFormat: 'YYYY-MM-DD HH:mm:ss',
      disabled: true
    }
  },
  {
    field: 'lastLoginIp',
    label: '上次登录IP',
    component: 'Input',
    remove: isAdd.value,
    componentProps: {
      placeholder: '请输入上次登录IP',
      clearable: true,
      disabled: true
    }
  },
  {
    field: 'nowLoginIp',
    label: '本次登录IP地址',
    component: 'Input',
    remove: isAdd.value,
    componentProps: {
      placeholder: '请输入本次登录IP地址',
      clearable: true,
      disabled: true
    }
  },
  {
    field: 'nowLoginDate',
    label: '本次登录时间',
    component: 'DatePicker',
    remove: isAdd.value,
    componentProps: {
      placeholder: '请选择本次登录时间',
      clearable: true,
      type: 'datetime',
      valueFormat: 'YYYY-MM-DD HH:mm:ss',
      disabled: true
    }
  },
  {
    field: 'loginCount',
    label: '登录次数',
    value: '0',
    remove: isAdd.value,
    component: 'Input',
    componentProps: {
      placeholder: '请输入登录次数',
      clearable: true,
      disabled: true
    }
  },
  {
    field: 'status',
    label: '账户状态',
    value: '0',
    component: 'Input',
    componentProps: {
      placeholder: '请输入账户状态',
      clearable: true
    }
  },
  {
    field: 'wx',
    label: '微信号',
    component: 'Input',
    componentProps: {
      placeholder: '请输入微信号',
      clearable: true
    }
  },
  {
    field: 'qq',
    label: 'QQ号',
    component: 'Input',
    componentProps: {
      placeholder: '请输入QQ号',
      clearable: true
    }
  },
  {
    field: 'remark',
    label: '备注信息',
    component: 'Input',
    componentProps: {
      placeholder: '请输入备注信息',
      clearable: true
    }
  },
  {
    field: 'expireTime',
    label: '账号过期时间',
    component: 'DatePicker',
    componentProps: {
      placeholder: '请选择账号过期时间',
      clearable: true,
      type: 'datetime',
      valueFormat: 'YYYY-MM-DD HH:mm:ss'
    }
  },
  {
    field: 'lockStartTime',
    label: '锁定开始时间',
    component: 'DatePicker',
    componentProps: {
      placeholder: '请选择锁定开始时间',
      clearable: true,
      type: 'datetime',
      valueFormat: 'YYYY-MM-DD HH:mm:ss'
    }
  },
  {
    field: 'lockEndTime',
    label: '锁定结束时间',
    component: 'DatePicker',
    componentProps: {
      placeholder: '请选择锁定结束时间',
      clearable: true,
      type: 'datetime',
      valueFormat: 'YYYY-MM-DD HH:mm:ss'
    }
  },
  // [[ OnflowGenerator Range (- inputSchema -) End ]]
  {
    field: 'createTime',
    label: '创建时间',
    component: 'Input',
    remove: isAdd.value,
    componentProps: {
      placeholder: '暂无创建时间',
      disabled: true
    }
  },
  {
    field: 'updateTime',
    label: '更新时间',
    component: 'Input',
    remove: isAdd.value,
    componentProps: {
      placeholder: '暂无更新时间',
      disabled: true
    }
  },
  {
    field: 'createUserName',
    label: '创建人',
    component: 'Input',
    remove: isAdd.value,
    componentProps: {
      placeholder: '暂无创建人',
      disabled: true
    }
  },
  {
    field: 'updateUserName',
    label: '更新人',
    component: 'Input',
    remove: isAdd.value,
    componentProps: {
      placeholder: '暂无更新人',
      disabled: true
    }
  }
])

const rules = reactive({
  // [[ OnflowGenerator Range (- rules -) Start ]]
  name: [
    required(),
    {
      validator: (_, value, callback) => {
        if (isBlank(value)) {
          callback()
          return
        }
        if (value.length > 50) {
          callback(new Error('账户名不能超过50个字符'))
        } else {
          callback()
        }
      }
    }
  ],
  nickName: [
    {
      validator: (_, value, callback) => {
        if (isBlank(value)) {
          callback()
          return
        }
        if (value.length > 50) {
          callback(new Error('昵称不能超过50个字符'))
        } else {
          callback()
        }
      }
    }
  ],
  password: [
    {
      validator: (_, value, callback) => {
        // 新增时必填
        if (isAdd.value && isBlank(value)) {
          callback(new Error('密码不能为空'))
          return
        }

        // 长度验证（新增和编辑时都适用）
        if (!isBlank(value)) {
          if (value.length > 32) {
            callback(new Error('密码不能超过32个字符'))
            return
          } else if (value.length < 6) {
            callback(new Error('密码长度不能小于6'))
            return
          }
        }

        callback()
      }
    }
  ],
  gender: [required()],
  locked: [required()],
  mobile: [
    {
      validator: (_, value, callback) => {
        if (isBlank(value)) {
          callback()
          return
        }
        if (value.length > 32) {
          callback(new Error('手机号不能超过32个字符'))
        } else {
          callback()
        }
      }
    }
  ],
  email: [
    {
      validator: (_, value, callback) => {
        if (isBlank(value)) {
          callback()
          return
        }
        if (value.length > 50) {
          callback(new Error('邮箱不能超过50个字符'))
        } else {
          callback()
        }
      }
    }
  ],
  avatar: [
    {
      validator: (_, value, callback) => {
        if (isBlank(value)) {
          callback()
          return
        }
        if (value.length > 255) {
          callback(new Error('头像不能超过255个字符'))
        } else {
          callback()
        }
      }
    }
  ],
  country: [
    {
      validator: (_, value, callback) => {
        if (isBlank(value)) {
          callback()
          return
        }
        if (value.length > 20) {
          callback(new Error('国家不能超过20个字符'))
        } else {
          callback()
        }
      }
    }
  ],
  province: [
    {
      validator: (_, value, callback) => {
        if (isBlank(value)) {
          callback()
          return
        }
        if (value.length > 20) {
          callback(new Error('省份不能超过20个字符'))
        } else {
          callback()
        }
      }
    }
  ],
  city: [
    {
      validator: (_, value, callback) => {
        if (isBlank(value)) {
          callback()
          return
        }
        if (value.length > 20) {
          callback(new Error('城市不能超过20个字符'))
        } else {
          callback()
        }
      }
    }
  ],
  address: [
    {
      validator: (_, value, callback) => {
        if (isBlank(value)) {
          callback()
          return
        }
        if (value.length > 255) {
          callback(new Error('地址不能超过255个字符'))
        } else {
          callback()
        }
      }
    }
  ],
  lastLoginIp: [
    {
      validator: (_, value, callback) => {
        if (isBlank(value)) {
          callback()
          return
        }
        if (value.length > 20) {
          callback(new Error('上次登录IP不能超过20个字符'))
        } else {
          callback()
        }
      }
    }
  ],
  nowLoginIp: [
    {
      validator: (_, value, callback) => {
        if (isBlank(value)) {
          callback()
          return
        }
        if (value.length > 20) {
          callback(new Error('本次登录IP地址不能超过20个字符'))
        } else {
          callback()
        }
      }
    }
  ],
  loginCount: [required()],
  status: [required()],
  wx: [
    {
      validator: (_, value, callback) => {
        if (isBlank(value)) {
          callback()
          return
        }
        if (value.length > 64) {
          callback(new Error('微信号不能超过64个字符'))
        } else {
          callback()
        }
      }
    }
  ],
  qq: [
    {
      validator: (_, value, callback) => {
        if (isBlank(value)) {
          callback()
          return
        }
        if (value.length > 32) {
          callback(new Error('QQ号不能超过32个字符'))
        } else {
          callback()
        }
      }
    }
  ],
  remark: [
    {
      validator: (_, value, callback) => {
        if (isBlank(value)) {
          callback()
          return
        }
        if (value.length > 2000) {
          callback(new Error('备注信息不能超过2000个字符'))
        } else {
          callback()
        }
      }
    }
  ]
  // [[ OnflowGenerator Range (- rules -) End ]]
})

defineExpose({
  submit
})
</script>

<template>
  <Form :rules="rules" @register="formRegister" :schema="schema" ref="formRef" />
</template>
